/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Cafechess.Chess.Controls
{
  /// <summary>
  /// Provides the ability to generate an image list for our background squares
  /// based off a color/s provided.
  /// </summary>
  public class SquareImageList : IDisposable
  {
    public ImageList SquareImages;
    public SquareImageList(Color highlight, Size aSize, float width)
    {
      SquareImages = new ImageList();
      SquareImages.ImageSize = aSize;    
      Bitmap bm;
#if POCKET_PC
      bm = new Bitmap(aSize.Width,aSize.Height); 
#else
      bm = new Bitmap( aSize.Width,aSize.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);      
#endif

      Graphics offScreenDC = Graphics.FromImage(bm);      
      Brush aBrush = new SolidBrush(Color.Magenta);
      offScreenDC.FillRectangle(aBrush,0,0,aSize.Width,aSize.Height);
      aBrush.Dispose();
#if POCKET_PC
      Pen aPen = new Pen(highlight);
#else
      Pen aPen = new Pen(highlight, width);
#endif 
      int x=0,y=0,w=aSize.Width, h=aSize.Height;
      if( width > 1 )
      {
        int offset = (int)(width / 2);
        x = (int)(width / 2);
        y = (int)(width / 2);
        w = w - (int)width;
        h = h - (int)width;
      }
      offScreenDC.DrawRectangle(aPen,x,y,w,h);
      aPen.Dispose();
#if POCKET_PC
      SquareImages.Images.Add(bm);      
#else
      SquareImages.Images.Add(bm,Color.Magenta);      
#endif
      offScreenDC.Dispose();
    }
    public SquareImageList(Color white, Color black, Size aSize)
    {
      SquareImages = new ImageList();
      SquareImages.ImageSize = aSize;      
#if POCKET_PC
      Bitmap bm = new Bitmap( aSize.Width,aSize.Height);
#else
      Bitmap bm = new Bitmap( aSize.Width,aSize.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
#endif
      Graphics offScreenDC = Graphics.FromImage(bm);
      Brush aBrush = new SolidBrush(white);      
      offScreenDC.FillRectangle(aBrush,0,0,aSize.Width,aSize.Height);
      aBrush.Dispose();
      SquareImages.Images.Add(bm);      
#if POCKET_PC
      bm = new Bitmap( aSize.Width,aSize.Height);
#else
      bm = new Bitmap( aSize.Width,aSize.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
#endif

      offScreenDC = Graphics.FromImage(bm);
      aBrush = new SolidBrush(black);
      offScreenDC.FillRectangle(aBrush,0,0,aSize.Width,aSize.Height);
      aBrush.Dispose();
      SquareImages.Images.Add(bm);
      offScreenDC.Dispose();
    }
    public SquareImageList(Color square, Size aSize, Color hatch)
    {
      SquareImages = new ImageList();
      SquareImages.ImageSize = aSize;
      //White square.
#if POCKET_PC
      Bitmap bm = new Bitmap( aSize.Width,aSize.Height);
#else
      Bitmap bm = new Bitmap( aSize.Width,aSize.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
#endif
      Graphics offScreenDC = Graphics.FromImage(bm);
      Brush aBrush = new SolidBrush(square);
      offScreenDC.FillRectangle(aBrush,0,0,aSize.Width,aSize.Height);
      aBrush.Dispose();
      offScreenDC.Dispose();
      SquareImages.Images.Add(bm);
      // Black square
#if POCKET_PC
      bm = new Bitmap( aSize.Width,aSize.Height);
#else
      bm = new Bitmap( aSize.Width,aSize.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
#endif      
      offScreenDC = Graphics.FromImage(bm);
#if POCKET_PC
      aBrush = new SolidBrush(square);// HatchBrush(HatchStyle.BackwardDiagonal,hatch,square);
#else
      aBrush = new HatchBrush(HatchStyle.BackwardDiagonal,hatch,square);
#endif      
      offScreenDC.FillRectangle(aBrush,0,0,aSize.Width,aSize.Height);
      aBrush.Dispose();
      offScreenDC.Dispose();
      SquareImages.Images.Add(bm);
      
    }
    #region IDisposable Members

    public void Dispose()
    {
#if POCKET_PC
#else
      SquareImages.Dispose();
#endif
    }

    #endregion
  }
}
